Tips
process.env types
Node treats all process.env variables as string
. One thing to remember is when we are using process.env
variables, they have the type string | undefined
. This can cause unforseen behavior to happen.
For example
SPM_RETRY_FORMAT: process.env.SPM_RETRY_FORMAT || 'hour',
Will always have the type `undefined | string`. That is because if SPM_RETRY_FORMAT is not specified in env-settings.sh, then it will be undefined. If you look in the generated .d.ts file, you will notice many variables have two types:
This can cause weird behaviors further down the pipeline especially since lt-config is used in most of the code-base.
To avoid this, we can always force a type on it. There are few scenarios in the variables we are seeing. So I did the various types for you here and you can then do it for the rest of the variables. See here:
// this will always force this as a type string instead of a type string | undefined
process.env.SENDBIRD_DEFAULT_USER = process.env.SENDBIRD_DEFAULT_USER ? process.env.SENDBIRD_DEFAULT_USER : 'Chaine_DEV'
// this will always force a type for WORKFLOW_SCHDEULED_TO_START_EXPIRE_MINS as number
process.env.WORKFLOW_EXPIRE_MINS = process.env.WORKFLOW_EXPIRE_MINS ? process.env.WORKFLOW_EXPIRE_MINS : '10'
WORKFLOW_SCHDEULED_TO_START_EXPIRE_MINS: parseFloat(process.env.WORKFLOW_EXPIRE_MINS),
// this will always make type for SPN_RETRY_SETTINGS as object (array is object type)
(process.env.SPN_RETRY_SETTINGS = process.env.SPN_RETRY_SETTINGS ? process.env.SPN_RETRY_SETTINGS : '[1, 1, 1]')
SPN_RETRY_SETTINGS: JSON.parse(process.env.SPN_RETRY_SETTINGS),
// this will always but type for DB_HOST as string
(process.env.DB_HOST = process.env.DB_HOST ? process.env.DB_HOST : '')
DB_HOST: process.env.DB_HOST
process.env.ENFORCE_SSL = process.env.ENFORCE_SSL === 'true' ? process.env.ENFORCE_SSL : 'false'
ENFORCE_SSL: process.env.ENFORCE_SSL === 'true' // this will make it boolean tru or boolean false
Static vs. Non-Static
The main difference between a static method/property and a non-static one is that: at the memory level, a portion of the memory will be created for the static fields, which will be shared across all objects in the class. So it works in C # or Java.
For javascript this behavior was implemented In ES6+. But for earlier versions of Ecma Scripts typescript emulates this case.
Static means that you want to access the method over the class name without creating an object (instantiation). It is also accessible from an outer class. Without static you need to create an object.
- Read this to learn more on static members and when to use them.
Global Variables
Global variables are declared outside the programming constructs. These variables can be accessed from anywhere within your code.
Implementing
- Create a types folder inside src and create a file global.d.ts
import {Raven} from 'lt-utils/dist/raven/raven'
declare global {
namespace NodeJS {
interface Global {
raven: Raven
}
}
}
- To access global variable use
global.raven
to access raven variable